package com.jkoss.wine.warehouse.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.merchant.service.IMerchantProductNormService;
import com.jkoss.wine.warehouse.entity.WareInventoryOrder;
import com.jkoss.wine.warehouse.service.IWareInventoryOrderService;
import com.jkoss.wine.warehouse.service.IWarehouseService;
import com.jkoss.wine.warehouse.vo.SystemInventoryVo;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;

/**
 * 仓库盘点单 前端控制器
 * 
 * @Author Jason
 * @Version 1.0, 2018-12-02
 * @See
 * @Since com.jkoss.wine.warehouse.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/warehouse/wareInventoryOrder")
public class WareInventoryOrderController extends BaseController {

	@Autowired
	private IWareInventoryOrderService iWareInventoryOrderService;
	@Autowired
	private IWarehouseService iWarehouseService;
	@Autowired
	private IMerchantProductNormService iMerchantProductNormService;

	@RequestMapping("/list")
	@RequiresPermissions("/warehouse/wareInventoryOrder/list")
	public String list(String onum, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		dwzPageBean.getCountResultMap().put("onum", onum);
		Wrapper wioWrapper = Condition.create().ne("wio.del", 1).like("wio.onum", onum);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wioWrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wioWrapper.orderBy("wio.add_time", false);
		}
		Page resultPage = iWareInventoryOrderService.selectVoByPage(dwzPageBean.toPage(), wioWrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "warehouse/wareInventoryOrder/list";
	}

	@RequestMapping("/toInsert")
	@RequiresPermissions("/warehouse/wareInventoryOrder/toInsert")
	public String toInsert(DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wioWrapper = Condition.create().ne("wi.del", 1);
		Page resultPage = iWareInventoryOrderService.selectVoSystemInventory(dwzPageBean.toPage(), wioWrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "warehouse/wareInventoryOrder/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/warehouse/wareInventoryOrder/toInsert")
	@ResponseBody
	public Object insert(@Valid WareInventoryOrder ware_inventory_order, String wiid[], Integer itat[], Integer psat[], HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 制单人
		ware_inventory_order.setSgps(getUserId());
		// 制单时间
		ware_inventory_order.setOdtm(CommonUtil.getDate());
		// 添加时间
		ware_inventory_order.setAddTime(CommonUtil.getDate());
		// 操作人
		ware_inventory_order.setOperator(getUserId());
		// 是否删除 0否 1是
		ware_inventory_order.setDel(0);

		if (iWareInventoryOrderService.insertWareInventoryOrder(ware_inventory_order, wiid, itat, psat)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	@RequestMapping("/toUpdate")
	@RequiresPermissions("/warehouse/wareInventoryOrder/toUpdate")
	public String toUpdate(String id, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iWareInventoryOrderService.selectById(id));
		Wrapper wioWrapper = Condition.create().ne("wi.del", 1).eq("wio.id", id);
		Page resultPage = iWareInventoryOrderService.selectVoSIinventory(dwzPageBean.toPage(), wioWrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "warehouse/wareInventoryOrder/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/warehouse/wareInventoryOrder/toUpdate")
	@ResponseBody
	public Object update(@Valid WareInventoryOrder ware_inventory_order, String wiid[], Integer itat[], Integer psat[], HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (iWareInventoryOrderService.updateWareInventoryOrder(ware_inventory_order, wiid, itat, psat)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/delete")
	@RequiresPermissions("/warehouse/wareInventoryOrder/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList<>();
		for (String string : id) {
			WareInventoryOrder entity = new WareInventoryOrder();
			entity.setId(string);
			entity.setDel(1);
			list.add(entity);
		}
		if (iWareInventoryOrderService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 订单详情
	 * 
	 * @param dwzPageBean
	 * @param wname
	 * @param ioid
	 * @param map
	 * @param str
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/details")
	@RequiresPermissions("/warehouse/wareInventoryOrder/details")
	public String details(DwzPageBean dwzPageBean, String wname, String id, ModelMap map, String str, HttpServletRequest request, HttpServletResponse response) {
		map.put("details", iWareInventoryOrderService.selectVoById(id));
		Wrapper wioWrapper = Condition.create().ne("wi.del", 1).eq("wio.id", id);
		Page resultPage = iWareInventoryOrderService.selectVoSIinventory(dwzPageBean.toPage(), wioWrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "warehouse/wareInventoryOrder/details";
	}

	/**
	 * 导出EXCEL
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/export")
	@RequiresPermissions("/warehouse/wareInventoryOrder/export")
	public String export(String id, ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create().ne("wio.del", 1).ne("wiod.del", 1).eq("wio.id", id);
		// 根据用户id获取角色
		// boolean isDirector = true;
		// List<Role> roles = getRoleList();
		// for (Role role : roles) {
		// if ("运营主管".equals(role.getName())) {
		// isDirector = false;
		// }
		// }
		// if (isDirector) {
		// wrapper.eq("maid", getUserId());
		// }
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);

		List list = iWareInventoryOrderService.selectVoSIinventory(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "盘点单", ExcelType.XSSF);
		// IExcelDataHandler dataHandler = new MoneyExcelHandler();
		// dataHandler.setNeedHandlerFields(new String[] { "订单金额" });
		// params.setDataHandler(dataHandler);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, SystemInventoryVo.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "盘点单");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

	/**
	 * 去导入页面
	 * 
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toImportExcel")
	@RequiresPermissions("/warehouse/wareInventoryOrder/toImportExcel")
	public String toImportExcel(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		return "/warehouse/wareInventoryOrder/importExcel";
	}

	/**
	 * 导入EXCEL
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/importExcel")
	@RequiresPermissions("/warehouse/wareInventoryOrder/toImportExcel")
	@ResponseBody
	public Object importExcel(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 1.构造导入参数
		ImportParams params = new ImportParams();
		// 指定title位置
		// 2.importExcelMore解释Excel
		ExcelImportResult<SystemInventoryVo> result = ExcelImportUtil.importExcelMore(file.getInputStream(), SystemInventoryVo.class, params);
		// 3.插入数据库
		List<SystemInventoryVo> systemInventoryVos = result.getList();
		if (!CommonUtil.isBlank(systemInventoryVos)) {
			iWareInventoryOrderService.insertExcel(systemInventoryVos, getUserId());

		} else {
			return ajaxError("数据内容为空！");
		}
		return ajaxSuccess();
	}

}
